#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using DataAccess = PHSRAG.Utility.DataAccess;
using System.Data.SqlClient;
#endregion

namespace PHSRAG.Insight21.COI
{
	/// <summary>
	/// COIOrganization.
	/// </summary>
	public class COIOrganization : BO.SmartBusinessObject
	{
		#region Constants
		private const string coiOrganizationKey = "COIOrganization_5EE36779-5C5A-4a9a-94C7-2ADA89A5A131";
		private const string ColumnId = "Id";
		private const string ColumnName = "Name";
		private const string ColumnDescription = "Description";
		private const string ColumnAdmPersonId = "AdmPersonId";
		private const string ColumnFullName = "FullName";
		private const string ColumnActive = "Active";
		private const string ColumnReviewed = "Reviewed";
		private const string ColumnDateCreated = "DateCreated";
		private const string ColumnDateModified = "DateModified";
		#endregion

		#region Instance Variables
		private int admPersonId;
		private string fullName;
		private string name;
		private string description;
		private bool active;
		private bool reviewed;
		private DateTime dateCreated;
		#endregion
		
		#region Statics
		/// <summary>
		/// Key used to place and/or retrieve this object from the cache.
		/// </summary>
		public static string COIOrganizationKey
		{
			get { return coiOrganizationKey; }
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the Person Id of the user who added the entity.
		/// </summary>
		public int AdmPersonId
		{
			get { return admPersonId; }
			set { SetDirty(ref admPersonId, value); }
		}


		/// <summary>
		/// Gets the name of the person who added the entity
		/// </summary>
		public string FullName
		{
			get { return fullName; }
		}


		/// <summary>
		/// Gets or sets the name of the entity
		/// </summary>
		public String Name
		{
			get { return name; }
			set { SetDirty(ref name, value); }
		}


		/// <summary>
		/// Gets or sets the description of the entity
		/// </summary>
		public string Description
		{
			get { return description; }
			set { SetDirty(ref description, value); }
		}


		/// <summary>
		/// Gets or sets a flag that indicates whether or not the entity details have been accepted
		/// </summary>
		public bool IsActive
		{
			get { return active; }
			set { SetDirty(ref active, value); }
		}


		/// <summary>
		/// Gets or sets a flag that indicates whether or not the entity is reviewed.
		/// </summary>
		public bool IsReviewed
		{
			get { return reviewed; }
			set { SetDirty(ref reviewed, value); }
		}

		
		/// <summary>
		/// Gets or sets the date on which the entity was created
		/// </summary>
		public DateTime DateCreated
		{
			get { return dateCreated; }
			set { SetDirty(ref dateCreated, value); }
		}

		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new COIOrganization object based on the Cache Policy passed in as a parameter. 
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		public COIOrganization(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		}


		/// <summary>
		/// Constructs a new COIOrganization object based on the Cache Policy passed in as a parameter. 
		/// All the instance variables will be set to the corresponding values from the dataros
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		/// <param name="dr">DataRow representing a COIOrganization</param>
		public COIOrganization(ICachePolicy cachePolicy, DataRow dr) : base(cachePolicy, dr)
		{
			PopulateMembers(dr);
		}
		#endregion

		#region SmartBusinessObject methods
		/// <summary>
		/// Cache Key uniquely identifies this object within the cache.
		/// </summary>
		/// <returns>the unique string for this object.</returns>
		public override string CacheKey()
		{
			return COIOrganizationKey;
		}
		
		
		/// <summary>
		/// Cache this instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(COIOrganizationKey, this);
		}

		
		/// <summary>
		/// Remove this instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(COIOrganizationKey);
		}
		
		
		/// <summary>
		/// Loads the COIOrganization object with the specified id.
		/// Note: This method is not yet implemented.
		/// </summary>
		/// <param name="connectionString">Information to connect to the database</param>
		/// <param name="args"></param>
		public override void Load(string connectionString, params object[] args)
		{
			throw new Exception("Not implemented");
		}
		
		
		/// <summary>
		/// Saves the state of the COIOrganization object to the database. 
		/// The stored procedure inserts a new record if the id of the object is 0, 
		/// otherwise the procedure updates the record with the matching COIOrganization id
		/// </summary>
		/// <param name="sqlTransaction">The context in which the insert/update should be executed</param>
		protected override void SaveObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			try
			{
				SqlParameter[] paramArray = DataAccess.BuildSqlParameterArray(
					"@id", SqlDbType.Int, Id, 
					"@name", SqlDbType.VarChar, Name,
					"@description", SqlDbType.VarChar, ((description == string.Empty) ? (object)DBNull.Value : description),
					"@admPersonId", SqlDbType.Int, admPersonId,
					"@active", SqlDbType.Bit, active,
					"@reviewed", SqlDbType.Bit, reviewed);
				
				
				Id = Convert.ToInt32 (DataAccess.ExecuteScalar( sqlTransaction.Connection, sqlTransaction, "SaveCOIOrganization", 
					paramArray));

			}
			catch( Exception e)
			{
				throw new Exception("Failed to save the COIOrganization", e);
			}
		}

		
		/// <summary>
		/// Deletes the ProtocolDocument from the database.
		/// Note: This method is not yet implemented.
		/// </summary>
		/// <param name="sqlTransaction">The transaction context in which the SQL statement is to be executed</param>
		protected override void DeleteObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			throw new Exception("Not implemented");
		}

		#endregion
		
		#region Public methods
		
		#endregion

		#region Private methods
		/// <summary>
		/// Populates instance fields with corresponding values in the datarow
		/// </summary>
		/// <param name="dataRow">DataRow holding the values to be assigned to instance variables</param>
		private void PopulateMembers(DataRow dataRow)
		{
			Id = (int)dataRow[ColumnId];

			admPersonId = (int)dataRow[ColumnAdmPersonId];
			name = (string)dataRow[ColumnName];
			fullName = (string) dataRow[ColumnFullName];
			active = (bool)dataRow[ColumnActive];
			reviewed = (bool)dataRow[ColumnReviewed];
			
			Object dbValue = dataRow[ColumnDescription];
			description = (dbValue == DBNull.Value) ? string.Empty : (string)dbValue;
			
			dateCreated = (DateTime)dataRow[ColumnDateCreated];
		}
		#endregion
	}
}
